<template>
  <div class="my-page">
    <!-- 顶部个人信息区域 -->
    <div class="user-info" v-if="isLogin">
      <div class="avatar">
        <img :src="userInfo.avatar" alt="用户头像">
      </div>
      <div class="username">{{ userInfo.username }}</div>
    </div>

    <!-- 功能模块区域 -->
    <div class="function-modules">
      <div class="module-item" @click="handleModuleClick('collection')">
        <i class="iconfont icon-collection"></i>
        <span>我的收藏</span>
      </div>
      <div class="module-item" @click="handleModuleClick('posts')">
        <i class="iconfont icon-posts"></i>
        <span>我的发布</span>
      </div>
      <div class="module-item" @click="handleModuleClick('wishlist')">
        <i class="iconfont icon-wishlist"></i>
        <span>我的心愿单</span>
      </div>
    </div>
  </div>

</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const isLogin = ref(false)
const userInfo = ref({
  username: '',
  avatar: ''
})

onMounted(() => {
  checkLoginStatus()
})

// 检查登录状态
const checkLoginStatus = () => {
  // 这里根据实际情况获取登录状态
  // const token = localStorage.getItem('token')
  // if (!token) {
  //   isLogin.value = false
  //   router.push('/login')
  // } else {
  //   isLogin.value = true
  //   getUserInfo()
  // }
}

// 获取用户信息
const getUserInfo = () => {
  // 这里调用接口获取用户信息
  userInfo.value = {
    username: '测试用户',
    avatar: 'default-avatar.png'
  }
}

// 处理模块点击
const handleModuleClick = (type) => {
  if (!isLogin.value) {
    router.push('/login')
    return
  }

  switch(type) {
    case 'collection':
      router.push('/my/collection')
      break
    case 'posts':
      router.push('/my/posts')
      break
    case 'wishlist':
      router.push('/my/wishlist')
      break
  }
}
</script>

<style scoped>
.my-page {
  padding: 20px;
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  overflow: hidden;
  margin-bottom: 10px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.username {
  font-size: 18px;
  font-weight: bold;
}

.function-modules {
  margin-top: 30px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.module-item {
  background-color: #f5f5f5;
  border-radius: 10px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
}

.module-item i {
  font-size: 24px;
  margin-bottom: 10px;
}

.module-item span {
  font-size: 14px;
}
</style>
